﻿using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
using Xc.DataAccess.Protocoals.WaterMeters.Services;
using Xc.WaterMeters.ServerHosts.Models;

namespace Xc.WaterMeters.ServerHosts.Services
{
    public class WaterMeterHostServices : IHostedService
    {
        private readonly IOptions<AppSettings> options;
        private readonly IWaterMeterSocketService waterMeterSocketService;

        public WaterMeterHostServices(IWaterMeterSocketService waterMeterSocketService, IOptions<AppSettings> options)
        {
            this.waterMeterSocketService = waterMeterSocketService;
            this.options = options;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {

            waterMeterSocketService.Option(new SuperSocket.ListenOptions
            {
                Port = options.Value.Options.Port,
                BackLog = options.Value.Options.BackLog,
                NoDelay = options.Value.Options.NoDelay,
                Ip = options.Value.Options.Ip,
                Path = options.Value.Options.Path,
                Security = options.Value.Options.Security,
            });
            await waterMeterSocketService.Start();
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
           await waterMeterSocketService.Stop();
        }
    }
}
